package com.intech.android.demo.cms.dao.impl;

import com.intech.android.demo.cms.bo.AudioContent;
import com.intech.android.demo.cms.bo.Category;
import com.intech.android.demo.cms.bo.Region;
import com.intech.android.demo.cms.common.Constants;
import com.intech.android.demo.cms.common.Util;
import com.intech.android.demo.cms.dao.interfaces.AudioContentDAO;
import com.intech.android.demo.cms.enums.Status;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import java.util.ArrayList;
import java.util.List;


public class AudioContentHibernateDAO extends CommonDAO implements AudioContentDAO
{

    public AudioContent getById(Integer id)
    {
        return (AudioContent) session.get(AudioContent.class, id);
    }

    public void save(AudioContent audioContent)
    {
        session.saveOrUpdate(audioContent);
    }

    public void delete(AudioContent audioContent)
    {
        session.delete(audioContent);
    }

    public void delete(Integer id)
    {
        AudioContent audioContent = getById(id);
        session.delete(audioContent);
    }

    public List<AudioContent> getAll()
    {
        Criteria criteria = session.createCriteria(AudioContent.class);
        List list = criteria.list();

        return list;
    }

    public List<AudioContent> getAll(Integer rows)
    {
        Criteria criteria = session.createCriteria(AudioContent.class).setMaxResults(rows);
        List list = criteria.list();

        return list;
    }

    public List<AudioContent> findAudioContentsByName(String name)
    {
        Criteria criteria = session.createCriteria(AudioContent.class);
        criteria.add(Expression.eq("name", name));
        List<AudioContent> filteredAudioContent = criteria.list();
        return filteredAudioContent;
    }

    public List<AudioContent> findAudioContentsByAdvertiser(String advertiser)
    {
        Criteria criteria = session.createCriteria(AudioContent.class);
        criteria.add(Expression.eq("advertiser", advertiser));
        List<AudioContent> filteredAudioContent = criteria.list();
        return filteredAudioContent;
    }

    public List<AudioContent> findAudioContents(List<Integer> regionsIds, List<Integer> categoriesId, Status status, Double chosenFrom, Double chosenTo)
    {
        List<AudioContent> filtered = new ArrayList<AudioContent>();
        Criteria criteria = session.createCriteria(AudioContent.class);
        if (!Util.isEmpty(regionsIds))
        {
            criteria.createCriteria("regions").add(Restrictions.in("id", regionsIds));

        }

        if (!Util.isEmpty(categoriesId))
        {
            criteria.createCriteria("category").add(Restrictions.in("id", categoriesId));
        }

        if (status != null)
        {
            if (status == Status.Inactive)
            {
                criteria.add(Restrictions.lt("budget", Constants.Logic.MIN_BUDGET));
            }
            else if (status == Status.Active)
            {
                criteria.add(Restrictions.ge("budget", Constants.Logic.MIN_BUDGET));
            }
        }

        if (chosenFrom != null && chosenTo != null)
        {
            criteria.add(Restrictions.between("budget", chosenFrom, chosenTo));
        }

        filtered = Util.uniqueFilter(criteria.list());
        return filtered;
    }

}























